package com.zlx.sharelive.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.DynamicListBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_base.base_util.TimeUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;
import com.zlx.sharelive.utils.NullTokenUtils;
import com.zlx.sharelive.utils.StringUt;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CommunityAc extends BaseAc {

    @BindView(R.id.rv_data)
    RecyclerView rv_data;
    @BindView(R.id.sw_layout)
    SwipeRefreshLayout sw_layout;
    @BindView(R.id.tv_title)
    TextView tv_title;

    private String title;
    private int page = 1;
    private String tag = "";
    private String isSelf = "1", isTj = "", city = "", izFee = "";
    private List<DynamicListBean.InfoDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<DynamicListBean.InfoDTO> mAdapter;
    private List<String> mImageList = new ArrayList<>();
    private CommonRecyclerViewAdapter<String> mImageAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_community;
    }

    @Override
    public void initViews() {
        super.initViews();

        title = getIntent().getStringExtra("title");
        tag = getIntent().getStringExtra("tag");

        tv_title.setText(title);

        initRecyclerView();
        getDynamicList();
    }

    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new CommonRecyclerViewAdapter<DynamicListBean.InfoDTO>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, DynamicListBean.InfoDTO entity, int position) {
                ImageView iv_community_head = holder.getView(R.id.iv_community_head);

                GlideImgManager.glideLoader(CommunityAc.this, entity.getAvatar(), iv_community_head, 0);

                holder.setText(R.id.tv_community_nickname, entity.getNickname());

                ImageView iv_community_vip = holder.getView(R.id.iv_community_vip);
                ImageView iv_community_goddess = holder.getView(R.id.iv_community_goddess);

                if (entity.getIsDz().equals("1")) {
                    ((ImageView) holder.getView(R.id.iv_fabulous_img)).setImageResource(R.mipmap.fabulous_img_true);
                } else {
                    ((ImageView) holder.getView(R.id.iv_fabulous_img)).setImageResource(R.mipmap.fabulous_img2);
                }

                try {
                    holder.setText(R.id.tv_community_time, TimeUtil.getTimeStringAutoShort2(TimeUtil.ConverToDate(entity.getDatetime()), true));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                holder.setText(R.id.tv_community_address, entity.getCity());
                holder.setText(R.id.tv_community_text_title, entity.getTitle());
                holder.setText(R.id.tv_community_text_content, entity.getText());
                holder.setText(R.id.tv_share_number, entity.getShares());
                holder.setText(R.id.tv_fabulous_number, entity.getLikes());
                holder.setText(R.id.tv_comment_number, entity.getComments());

//                holder.setText(R.id.tv_community_time, entity.getDatetime());

                mImageList.clear();
                if (!StringUt.isEmpty(entity.getThumbs())) {
                    mImageList.add(entity.getThumbs());
                }
                if (!StringUt.isEmpty(entity.getThumb2())) {
                    mImageList.add(entity.getThumb2());
                }
                if (!StringUt.isEmpty(entity.getThumb3())) {
                    mImageList.add(entity.getThumb3());
                }
                if (!StringUt.isEmpty(entity.getThumb4())) {
                    mImageList.add(entity.getThumb4());
                }
                if (!StringUt.isEmpty(entity.getThumb5())) {
                    mImageList.add(entity.getThumb5());
                }
                if (!StringUt.isEmpty(entity.getThumb6())) {
                    mImageList.add(entity.getThumb6());
                }
                if (!StringUt.isEmpty(entity.getThumb7())) {
                    mImageList.add(entity.getThumb7());
                }
                if (!StringUt.isEmpty(entity.getThumb8())) {
                    mImageList.add(entity.getThumb8());
                }
                if (!StringUt.isEmpty(entity.getThumb9())) {
                    mImageList.add(entity.getThumb9());
                }

                RecyclerView rv_community_image = holder.getView(R.id.rv_community_image);

                rv_community_image.setLayoutManager(new GridLayoutManager(CommunityAc.this, 4));

                mImageAdapter = new CommonRecyclerViewAdapter<String>(CommunityAc.this, mImageList) {
                    @Override
                    public void convert(CommonRecyclerViewHolder holder, String entity, int position) {
                        ImageView iv_community_image = holder.getView(R.id.iv_community_image);

                        GlideImgManager.glideLoader(CommunityAc.this, entity, iv_community_image, 1);
                    }

                    @Override
                    public int getLayoutViewId(int viewType) {
                        return R.layout.item_community_image_layout;
                    }
                };
                rv_community_image.setAdapter(mImageAdapter);
            }


            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_community_type2;
            }
        };

        rv_data.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                startActivity(new Intent(CommunityAc.this, CommunityDetailsAc.class)
                        .putExtra("id", mList.get(position).getId()));
            }
        });
    }

    private void getDynamicList() {
        ApiUtil.getServiceApi().getDynamicList(SPUtil.getToken(this),
                isSelf,
                "",
                city,
                izFee,
                tag,
                String.valueOf(page),
                "",
                "",
                isTj,
                "",
                "").observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<DynamicListBean>>() {
            @Override
            public void onSuccess(ApiResponse<DynamicListBean> data) {
                sw_layout.setRefreshing(false);
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            if (null != data.getData().getInfo()) {
                                mList.addAll(data.getData().getInfo());
                            }
                        } else {
                            NullTokenUtils.isNullToken(data.getData().getCode(), data.getData().getMsg());
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        }));
    }

    @OnClick(R.id.iv_back)
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

}
